package org.github.choviwu.example.common.common;

import com.jcraft.jsch.ChannelSftp;
import org.github.choviwu.example.common.service.SysConfigService;
import org.github.choviwu.example.common.util.DownLoadUtils;
import org.github.choviwu.example.common.util.SFTPUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;

/**
 * Created by ChoviWu on 2018/03/18
 * Description:
 */
@RestController
public class UploadController {

    static String[] picList = {"jpg","png","jpeg","gif","em"};
    private final SysConfigService configService;
    @Autowired
    UploadController(SysConfigService configService){
        this.configService = configService;
    }
    /**
     * 上传文件
     * @param file
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public Object upload(MultipartFile file, HttpServletRequest request)  {
        String originName = file.getOriginalFilename();
        this.checkFileName(originName);
        String path = request.getServletContext().getRealPath("/");
        String dirPath = "upload/";
        String tempPath = path+dirPath;
        //后缀
        String end = originName.substring(originName.indexOf("."),originName.length());
        String  name = new Date().getTime()+end;
        File dir = new File(tempPath);
        File temp  =  new File(tempPath+name);
        if(!dir.exists()){
            dir.mkdir();
        }
        if(!temp.exists()){
            try {
                temp.createNewFile();
                file.transferTo(temp);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        System.out.println(dirPath+name);
        SFTPUtils sftpUtils = new SFTPUtils();
        //连接服务器
        ChannelSftp sftp=sftpUtils.connect(configService.getValueByKey("sftp_host"),
                Integer.parseInt(configService.getValueByKey("sftp_port")), configService.getValueByKey("sftp_uname"),
                configService.getValueByKey("sftp_password"));
        //上传
        sftpUtils.upload("/data/images/images/",tempPath+name,sftp);
        return name;
    }

    /**
     * 文件下载
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/download",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object downLoad(HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Content-Disposition","attachment;fileName="+request.getParameter("UID"));
        response.setContentType("multipart/form-data");
        //TODO 文件路径
        String dirPath = request.getServletContext().getRealPath("/")+"upload/";
        return DownLoadUtils.downLoad("",dirPath);
    }

    private  void checkFileName(String fileName){

        fileName = fileName.substring(fileName.lastIndexOf(".")+1);
        for (String name : picList){
            if(name.equals(fileName)){
                return;
            }
        }
    }

}
